package eu.web.tag;


import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;

import eu.news.template.StaticHelper;

public class Static extends BodyTagSupport {

	String key=null;

	private static final long serialVersionUID = 7923066072371460324L;
 
	@Override
	public int doAfterBody() throws JspException {
		String context = getBodyContent().getString();
		String sort = (String) pageContext.getRequest().getAttribute(StaticHelper.STATIC_SORT_KEY);
		Integer id = (Integer) pageContext.getRequest().getAttribute(StaticHelper.STATIC_ID_KEY);
		if (sort!=null && id != null)
			try {
				StaticHelper.write(sort, id, context);
			} catch (IOException e) {
				e.printStackTrace();
			}
		return super.doAfterBody();
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write(getBodyContent().getString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {
		return BodyTag.EVAL_BODY_BUFFERED;
	}

	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key the key to set
	 */
	public void setKey(String key) {
		this.key = key;
	}


}
